<?php
/**
 * 批量获取item。本质上是getItem的包装器。在DynamoDb的服务器上，还是会执行多次getItem。
 * 这样的好处是节省网络请求，让DynamoDb来处理并发问题
 * 最多能包含100条GetItem请求，或取出最多16MB数据
 * author: JiangJun
 * CreateTime:2021/7/25 22:28
 */


require '../vendor/autoload.php';

use Aws\DynamoDb\Exception\DynamoDbException;
use Aws\DynamoDb\Marshaler;

$sdk = new Aws\Sdk([
    'region'   => 'us-west-2',
    'version'  => 'latest'
]);

$tableName = 'Movies';
$dynamodb = $sdk->createDynamoDb();
$marshaler = new Marshaler();


$params = [
    'RequestItems'=>[
        $tableName =>[
            'Keys' => [
                //查询2条数据，作为一次请求来发送。这里的title，必须指定，否则会报错
                $marshaler->marshalItem(['year'=>2007,'title'=>'3:10 to Yuma']),
                $marshaler->marshalItem(['year'=>2013,'title'=>'The Hunger Games: Catching Fire']),
            ],
            'ProjectionExpression'=>'title,info'
        ],

    ],
];

try {
    $result = $dynamodb->batchGetItem($params);
    foreach ($result['Responses'][$tableName] as $v) {
        print_r($marshaler->unmarshalItem($v));
    }

} catch (DynamoDbException $e) {
    echo "Unable to batch get item:\n";
    echo $e->getMessage() . "\n";
}